<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
    <title>账号</title>
</head>
<body>
<div class="pageheader">
    <h2>账号</h2>
</div>
<div class="contentpanel">
    <div class="panel panel-default">
        <form:form action="${contextPath}/admin/user/update.htm" method="post" commandName="formDto"
                   cssClass="form-horizontal form-bordered">
            <div class="form-group">
                <label class="col-sm-3 control-label">用户</label>

                <div class="col-sm-6">
                    <input type="text" class="form-control" value="${formDto.userDto.name}" id="search-input"
                           placeholder="请输入用户名称搜索" ${not empty formDto.userDto.name?"disabled='disabled'":""}/>
                    <form:hidden path="userId" id="userId"/>
                    <form:errors path="userId" cssClass="error text-danger"/>
                    <form:hidden path="userDto.name"/>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">账号</label>
                <c:if test="${ empty formDto.uuid}">
                    <div class="col-sm-6">
                        <form:input path="username" cssClass="form-control" placeholder="账号"/>
                        <form:errors path="username" cssClass="error text-danger"/>
                    </div>
                </c:if>

                <c:if test="${ not empty formDto.uuid}">
                    <div class="col-sm-6">
                        <form:hidden path="username"/>
                        <form:hidden path="uuid"/>
                        <p class="form-control-static">${formDto.username}</p>
                    </div>
                </c:if>
            </div>

            <c:if test="${empty formDto.uuid}">
                <div class="form-group">
                    <label class="col-sm-3 control-label">密码</label>

                    <div class="col-sm-6">
                        <input type="password" name="password" class="form-control" id="exampleInputPassword3"
                               placeholder="密码"/>
                        <form:errors path="password" cssClass="error text-danger"/>
                    </div>
                </div>
            </c:if>

            <div class="form-group">
                <label class="col-sm-3 control-label">权限</label>

                <div class="col-sm-6">
                    <div class="checkbox block">
                        <c:forEach items="${formDto.allPrivilege}" var="allItem">
                            <label style="display:inline-block;width:250px;">
                                <input type="checkbox" name="privilegeList" value="${allItem}"
                                        <c:forEach items="${formDto.privilegeList}" var="item">
                                            ${item eq allItem?"checked='checked'":""}
                                        </c:forEach>
                                        />${allItem.label}
                            </label>
                        </c:forEach>
                    </div>
                    <c:forEach items="${formDto.allPrivilege}" var="allItem" varStatus="status">
                        <input type="hidden" name="allPrivilege[${status.index}]" value="${allItem}">
                        <input type="hidden" name="allPrivilege[${status.index}].label" value="${allItem.label}">
                    </c:forEach>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label"></label>

                <div class="col-sm-6">
                    <button type="submit" class="btn btn-primary">提交</button>
                    <a href="javascript:window.history.go(-1);" class="btn btn-default">取消</a>
                </div>
            </div>
        </form:form>
    </div>
</div>
<script type="text/javascript">
    $("input#search-input").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/admin/user/search_list",
                dataType: "json",
                data: {
                    key: request.term
                },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.name,
                            value: item.name,
                            uuid: item.uuid
                        }
                    }));
                }
            });
        },
        minLength: 1,
        select: function (event, ui) {
            if (ui.item) {
                $("#userId").val(ui.item.uuid);
            }
        }
    });
</script>
</body>
</html>
